﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _02_列表
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Racer> racerlist = new List<Racer>(12);//设置列表张元素的个数
            Console.WriteLine(racerlist.Capacity);//也可以通过List.Capacity的属性：输出111
            Racer racer = new Racer(122, "Tim", "Jhson", "English", 12);
            racerlist.Add(new Racer(122, "Tim", "Jhson", "English", 12));//添加一个元素
            racerlist.Insert(1,new Racer(111, "I", "Chik", "China", 23));//插入必要的位置(和添加不同的地方就在一个参数):只能往下一个null的地方插入，bug
           racerlist.AddRange(new Racer[]{
               new Racer(2, "Tim", "Jhson", "English", 12),
               new Racer(3, "Tim", "Jhson", "English", 12),
               racer
           });//数组的形式大量插入
            Console.WriteLine(racerlist[1].Id);//通过索引值来访问某个元素：类似于数组
            Console.WriteLine(racerlist.Count);//实际包含了多少个元素
            racerlist.RemoveAt(1);//删除索引号位置的元素
            //第一种方式
            //for (int i = 0; i < racerlist.Count; i++)
            //{
            //    Console.WriteLine(racerlist[i].Id);
            //}
            ////第二种方式
            //foreach (Racer item in racerlist)
            //{
            //    Console.WriteLine(item.Id);
            //}
            //第三种方式
            //racerlist.ForEach(Console.WriteLine);
           
            //第四种方式，lmbda方式
            racerlist.ForEach(r => Console.WriteLine( r));
            racerlist.IndexOf(racer);//IndexOf：返回的对象的元素索引值，比较的是对象的引用地址,所以你new一个对象的出来的话返回的肯定是-1，不在里面
            racerlist.FindIndex(new FindId(122).FindIdPredicate);//Predicate<T>:坑爹啊  
            //1.委托绑定这个方法FindIdPredicate(Racer r)对对象的判别
            //2.提取racerlist对象的下的数组，for循环，用委托运行1方法，返回true就返回一个int值，坑爹
            racerlist.FindAll(new FindId(122).FindIdPredicate);//原来这就是微软，一句话：坑爹
            racerlist.Sort();//排序没有写，这块懒得写了
            Console.ReadKey();
        }
    }
    public class Racer:IComparable<Racer>,IFormattable
    {
        public int Id { get; private set; }
        public string  FirstName { get; private set; }
        public string LastName { get; private set; }
        public string Country { get; private set; }
        public int Wins { get; set; }
        public Racer(int id,string firstname,string lastname,string country,int wins)
        {
            this.Id = id;
            this.FirstName = firstname;
            this.LastName = lastname;
            this.Country = country;
            this.Wins = wins;
        }
        public override string ToString()
        {
           // Console.WriteLine("来自{0}的Id：{1}的{2}的选手曾经获得了{3}冠军",this.Country,this.Id,this.FirstName+this.LastName,this.Wins);
            return string.Format("Id:{0},FirstName:{1},LastName:{2},Country:{3},Wins:{4}", this.Id, this.FirstName, this.LastName , this.Country, this.Wins);
        }

        public int CompareTo(Racer other)
        {
            int result = this.FirstName.CompareTo(other.FirstName);
            if (result==0)
            {
                result = this.LastName.CompareTo(other.LastName);
            }
            return result;
        }
        public string ToString(string format)
        {
            return ToString(format, null);
        }
        public string ToString(string format,IFormatProvider formatProvider)
        {
            if (format == null) format = "N";
            switch (format.ToUpper())
            {
                case "N":
                    return ToString();
                case "F":
                    return this.FirstName;
                case "L":
                    return this.LastName;
                case "W":
                    return this.Wins.ToString();
                case "C":
                    return this.Country;
                default:
                    return "Null";

            }
            
        }

        
    }
    public class FindId
    {
        private int Id;
        public FindId(int id)
        {
            this.Id = id;
        }
        public bool FindIdPredicate(Racer r)
        {
            if (r == null) throw new ArgumentNullException();
            return r.Id == this.Id;
            
        }
    }
    
}
